package edu.nju.desserthouse.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.nju.desserthouse.action.business.VipAddrInfoBean;
import edu.nju.desserthouse.action.business.VipBirthInfoBean;
import edu.nju.desserthouse.action.business.VipInfoBean;
import edu.nju.desserthouse.dao.VipDao;

/**
 * Servlet implementation class VipServlet
 */
@WebServlet("/vipstatistics.manager")
public class VipServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
	@EJB
	VipDao vipDao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VipServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		execute (request, response);
	}

	public void execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		double unknown = 0;
		double male = 0;
		double female = 0;
		VipInfoBean vipInfo = new VipInfoBean();
		vipInfo.setVipList(vipDao.getVipList());
		for(int i = 0; i < vipInfo.getListSize(); i++){
			if(vipInfo.getGender(i + 1) == 0){
				unknown++;
			}else if(vipInfo.getGender(i + 1) == 1){
				male++;
			}else if(vipInfo.getGender(i + 1) == 2){
				female++;
			}else{
				System.out.println("Data Error!!!");
			}
		}
		vipInfo.setNotknown(Integer.parseInt(new java.text.DecimalFormat("0").format(unknown)));
		vipInfo.setMale(Integer.parseInt(new java.text.DecimalFormat("0").format(male)));
		vipInfo.setFemale(Integer.parseInt(new java.text.DecimalFormat("0").format(female)));
		
		double unknownRatio = 0;
		double maleRatio = 0;
		double femaleRatio = 0;
		
		double gender = unknown + male + female;
		if(gender != 0){
			unknownRatio = unknown/gender * 100;
			maleRatio = male/gender * 100;
			femaleRatio = female/gender * 100;
		}else{
			System.out.println("No Vip!!!");
		}
		vipInfo.setNotknownRatio(unknownRatio);
		vipInfo.setMaleRatio(maleRatio);
		vipInfo.setFemaleRatio(femaleRatio);
		
		req.getSession().setAttribute("vipInfo", vipInfo);
		
		VipAddrInfoBean addrInfo = new VipAddrInfoBean();
		addrInfo.setAddrList(vipDao.getAddressList());
		addrInfo.initAddrNumList();
		
		for(int i = 0; i < vipInfo.getListSize(); i++){
			for(int j = 0; j < addrInfo.getAddrSize(); j++){
				if((vipInfo.getAddr(i)).equals(addrInfo.getAddr(j))){
					addrInfo.addToNum(j);
					break;
				}
			}
		}
		
		double address = 0;
		for(int i = 0; i < addrInfo.getAddrSize(); i++){
			address += addrInfo.getAddrNum(i);
		}
		
		if(address != 0){
			for(int i = 0; i < addrInfo.getAddrSize(); i++){
				double ratio = addrInfo.getAddrNum(i)/address * 100;
				addrInfo.setRatio(i, ratio);
			}
		}else{
			System.out.println("No VIP!!!");
		}
		
		req.getSession().setAttribute("addrInfo", addrInfo);

		VipBirthInfoBean birthInfo = new VipBirthInfoBean();
		birthInfo.initBirthNumList();
		
		for(int i = 0; i < vipInfo.getListSize(); i++){
			for(int j = 0; j < birthInfo.getBirthSize() - 1; j++){
				boolean lower = ((vipInfo.getBrith(i).toString()).compareTo(birthInfo.getBirth(j).toString()) >=0);
				boolean upper = ((vipInfo.getBrith(i).toString()).compareTo(birthInfo.getBirth(j + 1).toString()) <=0);
				if(lower && upper){
					birthInfo.addToNum(j);
					break;
				}
			}
			boolean lower = ((vipInfo.getBrith(i).toString()).compareTo(birthInfo.getBirth(birthInfo.getBirthSize() - 1).toString()) >=0);
			if(lower){
				birthInfo.addToNum(birthInfo.getBirthSize() - 1);
			}
		}
		
		double birth = 0;
		for(int i = 0; i < birthInfo.getBirthSize(); i++){
			birth += birthInfo.getBirthNum(i);
		}
		
		if(birth != 0){
			for(int i = 0; i < birthInfo.getBirthSize(); i++){
				double ratio = birthInfo.getBirthNum(i)/birth * 100;
				birthInfo.setRatio(i, ratio);
			}
		}else{
			System.out.println("No VIP!!!");
		}

		req.getSession().setAttribute("birthInfo", birthInfo);
		
		resp.sendRedirect("/desserthouseWEB/manager/vipstatistics.jsp");
	}
}
